<?php

namespace app\api\controller\fp;

use app\api\validate\fp\PostCommentsValidate;
use app\api\validate\fp\PostValidate;
use app\common\controller\Frontend;
use app\logic\fp\PostCommentLogic;
use app\logic\fp\PostLogic;
use RuntimeException;
use think\facade\Log;
use Throwable;

class Comment extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();

    }

    /**
     * 评论动态
     */
    public function comment()
    {
        $params = $this->request->post();
        $validate = new PostCommentsValidate();

        try {
            $validate->scene('comment')->check($params);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }

        try {
            $post = PostLogic::getInstance()->checkExist($params['post_id'], $params['creator_id']);
            if (!$post) {
                throw new RuntimeException(__('post not found'));
            }
            $params['comment_user_id'] = $this->auth->id;
            $res = PostCommentLogic::getInstance()->comment($params);
        } catch (RuntimeException $e) {
            $this->error($e->getMessage());
        } catch (Throwable $e) {
            $message = config('app.env') == "prod" ? __('server error') : $e->getMessage();
            Log::channel('my')->error($e->getMessage());
            $this->error($message);
        }

        $this->success(__('comment success'), $res);
    }

    public function list()
    {
        $params = $this->request->post();
        $validate = new PostCommentsValidate();

        try {
            $validate->scene('list')->check($params);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }

      //  try {
            $res = PostCommentLogic::getInstance()->list($params['post_id'], $this->auth->id);
/*        } catch (RuntimeException $e) {
            $message = config('app.env') == "prod" ? __('server error') : $e->getMessage();
            Log::channel('my')->error($e->getMessage());
            $this->error($message);
        }*/

        $this->success(__('comment success'), $res);
    }
}